using System;
using System.Collections.Generic;
using System.IO;


namespace AHWrapper{
	public class Board{
		public Location woods;
		public Location southSquare;
		public List<Location> allLoc;
		public List<Mons> monsBag;
		public List<Mons> monsInPlay;
		
		public Board(){
			createMonsBag();
			allLoc = new List<Location>();

			
			allLoc.Add(new Location("Woods","Southside"));
			allLoc.Add(new Location("South Square","Southside"));
			allLoc.Add(new Location("Hospital","Southside"));
			allLoc.Add(new Location("Sanitarium","Southside"));
			
			allLoc[0].adjacent[Constants.North]=allLoc[1];
			allLoc[0].adjacent[Constants.East]=allLoc[2];
			allLoc[0].stable=false;
			
			allLoc[1].adjacent[Constants.South]=allLoc[0];
			allLoc[1].adjacent[Constants.East]=allLoc[3];
			allLoc[1].stable=false;
			
			allLoc[2].adjacent[Constants.North]=allLoc[3];
			allLoc[2].adjacent[Constants.West]=allLoc[0];
			allLoc[2].stable=true;
			
			allLoc[3].adjacent[Constants.South]=allLoc[2];
			allLoc[3].adjacent[Constants.West]=allLoc[1];
			allLoc[3].stable=true;
		}
		
		public void move(Investigator character, int pDestination){
			Location lCurLoc=getLoc(character);
			lCurLoc.adjacent[pDestination].addInvestigator(character);
			lCurLoc.removeInvestigator(character);
			character.setPostion(lCurLoc.adjacent[pDestination].name);
			
		}
		
		public void move(Investigator pInvest, string pDestination){
			Location lDest=getLocByName(pDestination);
			lDest.addInvestigator(pInvest);
			getLoc(pInvest).removeInvestigator(pInvest);
			pInvest.setPostion(lDest.name);
		}
		
		public Location getLoc(Investigator pInvestigator){
			for(int x=0; x<allLoc.Count; x++){
				if(String.Compare(pInvestigator.curLocName,allLoc[x].name)==0){
					return allLoc[x];
				}
			}
			return allLoc[0];
		}
		public bool placeGate(Gate pOpenGate, string pLocName){
			Location gLoc=getLocByName(pLocName);
			return gLoc.addGate(pOpenGate);
		}
		public Location getLocByName(string pLocName){
			for(int x=0; x<allLoc.Count; x++){
				if(String.Compare(pLocName, allLoc[x].name)==0){
					return allLoc[x];
				}
			}
			return null;
		}
		public void spawn(){
			foreach(Location loc in allLoc){
				if(loc.isGate()){
					loc.addOccupant(selectMon());
				}
			}
		}
		//assemle the mons list
		public void createMonsBag(){
			//create mons list
			monsInPlay = new List<Mons>();
			monsBag = new List<Mons>();
			
			//Open file containing mons data
			TextReader tr = new StreamReader("monsts.txt");
			
			//Read file by line, create mons and add it to the deck
			string line;
			string[] arInfo = new string[8];
			while((line=tr.ReadLine())!=null){
				arInfo = line.Split(',');
				monsBag.Add(new Mons(arInfo[0],Convert.ToInt32(arInfo[1]),Convert.ToInt32(arInfo[2]),Convert.ToInt32(arInfo[3]),Convert.ToInt32(arInfo[4]),Convert.ToInt32(arInfo[5]),
				                                Convert.ToInt32(arInfo[6]),Convert.ToInt32(arInfo[7]),Convert.ToInt32(arInfo[8])));
			}
			Console.WriteLine("mons in bag");
			Console.WriteLine(monsBag.Count.ToString());
			//close the file
			tr.Close();
		}
		//randomly retrieve mons from "bag"
		public Mons selectMon(){
			Random rand = new Random();
			Console.WriteLine("mons in bag");
			Console.WriteLine(monsBag.Count.ToString());
			int x = rand.Next(0,monsBag.Count-1);
			Mons selectedMons=monsBag[x];
			//move mons from "bag" to in play list
			monsInPlay.Add(selectedMons);
			monsBag.Remove(selectedMons);
			return selectedMons;
		}
		
		public int encTest(Location pLoc){
			return pLoc.occupantCount();
		
		}
		
		public Mons getOccupant(Location pLoc, int pIndex){
			return pLoc.getOccupant(pIndex);
		}
		
		public Mons removeOccupant(Location pLoc, int pIndex){
			return pLoc.removeOccupant(pIndex);
		}	
	}
}